<template>
  <div>
    <my-breadcrumb :items="['社区管理', '话题分类管理']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">添加分类</el-button>
    </div>
    <el-table :data="classifies">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="分类名称"
        prop="value">
      </el-table-column>
      <el-table-column
        label="话题图片"
        prop="img">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" width="40px" height="40px">
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增分类' : '信息修改'" :visible.sync="showAdd">
      <el-form :model="classify" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="分类名称" prop="value">
          <el-input v-model="classify.value"/>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="classify.img" :src="classify.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRow} from '../../utils/getList'

export default {
  name: 'topic-classify',
  data () {
    return {
      searchItems: [
        {
          label: '分类名称',
          type: 'input',
          key: 'value'
        }
      ], // 检索结构
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      classifies: [],
      classify: {
        value: '',
        img: ''
      },
      rules: {
        value: [
          { required: true, message: '请输入话题分类名称', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请输入话题分类图片', trigger: 'change' }
        ]
      },
      isAdd: true,
      showAdd: false,
      updateRow: null
    }
  },
  methods: {
    // /Topiclabel/index
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize}
      this.axios.get('/Topiclabel/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.classifies = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.classify.img = ev.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
        this.$refs['addForm'].clearValidate()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.classify).forEach(key => {
          this.classify[key] = row[key]
        })
        this.$refs['addForm'].clearValidate()
      })
    },
    toSubmit () {
      let url = '/Topiclabel/add'
      let params = {}
      Object.keys(this.classify).forEach(key => {
        params[key] = this.classify[key]
      })
      if (!this.isAdd) {
        url = '/Topiclabel/edit'
        params.id = this.updateRow.id
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.getList()
                this.showAdd = false
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          this.addLoading = false
          return false
        }
      })
    },
    toDelete (id) {
      deleteRow({url: '/Topiclabel/delete', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
